package com.yough.api.framework.core.message.process;

import com.yough.api.framework.core.message.IMessageHandler;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;

/**
 * @Description
 * @Author yough
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2021/1/14
 */
public class XmlMessageHandler implements IMessageHandler {

    @Override
    public Element getServiceHeaderNode(String xmlMessage) throws Exception {
        return this.getElementNodeByNodeName(xmlMessage, "Service_Header");
    }

    @Override
    public Element getServiceBodyNode(String xmlMessage) throws Exception {
        return this.getElementNodeByNodeName(xmlMessage, "Service_Body");
    }

    @Override
    public Element getRequestNode(String xmlMessage) throws Exception {
        return this.getElementNodeByNodeName(xmlMessage, "request");
    }

    @Override
    public Element getResponseNode(String xmlMessage) throws Exception {
        return this.getElementNodeByNodeName(xmlMessage, "response");
    }

    @Override
    public Element getExtAttributesNode(String xmlMessage) throws Exception {
        return this.getElementNodeByNodeName(xmlMessage, "ext_attributes");
    }

    /**
     * 根据节点名称获取该节点信息
     *
     * @param xmlMessage
     * @param nodeName
     * @return
     */
    private Element getElementNodeByNodeName(String xmlMessage, String nodeName) throws Exception {
        // 读取xml串
        Document doc = DocumentHelper.parseText(xmlMessage);
        // 获取根节点
        Element rootElement = doc.getRootElement();
        // 拿到根节点的名称
//        System.out.println("根的名称：" + rootElement.getName());
        // 获取根节点下的子节点
        Iterator rootIter = rootElement.elementIterator();
        while (rootIter.hasNext()) {
            // 子节点
            Element nodeElement = (Element) rootIter.next();
            if (("Service_Header").equals(nodeElement.getName())) {
                // 解析header节点
//                Iterator headerNodeIter = nodeElement.elementIterator();
//                while (headerNodeIter.hasNext()) {
//                    Element headerNode = (Element) headerNodeIter.next();
//                    System.out.println("header param: " + headerNode.getName() + "->" + headerNode.getText());
//                }
                if ("Service_Header".equals(nodeName)) {
                    return nodeElement;
                }
            } else if (("Service_Body").equals(nodeElement.getName())) {
                // 解析body节点
                Iterator nodeItr = nodeElement.elementIterator();
                while (nodeItr.hasNext()) {
                    Element node = (Element) nodeItr.next();
                    if (("request").equals(node.getName())) {
                        // 解析请求参数节点
//                        Iterator<Element> requestParamIter = node.elementIterator();
//                        while (requestParamIter.hasNext()) {
//                            Element requestParamNode = (Element) requestParamIter.next();
//                            System.out.println("request param: " + requestParamNode.getName() + "->" + requestParamNode.getText());
//                        }
                        if ("request".equals(nodeName)) {
                            return node;
                        }
                    } else if (("response").equals(node.getName())) {
                        // 解析响应参数节点
//                        Iterator<Element> responseResultIter = node.elementIterator();
//                        while (responseResultIter.hasNext()) {
//                            Element responseResultNode = (Element) responseResultIter.next();
//                            System.out.println("response result: " + responseResultNode.getName() + "->" + responseResultNode.getText());
//                        }
                        if ("response".equals(nodeName)) {
                            return node;
                        }
                    } else if (("ext_attributes").equals(node.getName())) {
                        // 解析扩展域节点
                        if ("ext_attributes".equals(nodeName)) {
                            return node;
                        }
                    }
                }
                if ("Service_Body".equals(nodeName)) {
                    return nodeElement;
                }
            }
        }
        return null;
    }

    @Override
    public Class<?> convertMessageToBean(String xmlMessage, Class<?> clazz) throws Exception {
        // 读取xml串
        Document doc = DocumentHelper.parseText(xmlMessage);
        // 获取根节点
        Element rootElement = doc.getRootElement();
        // 拿到根节点的名称
        System.out.println("根的名称：" + rootElement.getName());
        // 获取根节点下的子节点
        Iterator rootIter = rootElement.elementIterator();
        while (rootIter.hasNext()) {
            // 子节点
            Element nodeElement = (Element) rootIter.next();
            if (("Service_Header").equals(nodeElement.getName())) {
                // 解析header节点
                Iterator headerNodeIter = nodeElement.elementIterator();
                while (headerNodeIter.hasNext()) {
                    Element headerNode = (Element) headerNodeIter.next();
                    System.out.println("header param: " + headerNode.getName() + "->" + headerNode.getText());
                }
            } else if (("Service_Body").equals(nodeElement.getName())) {
                // 解析body节点
                Iterator nodeItr = nodeElement.elementIterator();
                while (nodeItr.hasNext()) {
                    Element node = (Element) nodeItr.next();
                    if (("request").equals(node.getName())) {
                        // 解析请求参数节点
                        Iterator<Element> requestParamIter = node.elementIterator();
                        while (requestParamIter.hasNext()) {
                            Element requestParamNode = (Element) requestParamIter.next();
                            System.out.println("request param: " + requestParamNode.getName() + "->" + requestParamNode.getText());
                            // 将element元素解析成class类对应的实体
                            this.doConvertElementToBean(requestParamNode, clazz);
                        }
                    } else if (("response").equals(node.getName())) {
                        // 解析响应参数节点
                        Iterator<Element> responseResultIter = node.elementIterator();
                        while (responseResultIter.hasNext()) {
                            Element responseResultNode = (Element) responseResultIter.next();
                            System.out.println("response result: " + responseResultNode.getName() + "->" + responseResultNode.getText());
                        }
                    } else if (("ext_attributes").equals(node.getName())) {
                        // 解析扩展域节点

                    }
                }
            }
        }
        return null;
    }

    private void doConvertElementToBean(Element element, Class<?> clazz) {
        // 根据
    }

    @Override
    public <T> String convertBeanToMessage(T Bean) {
        return null;
    }

}
